//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SuperFundPayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (Double strike, Double secondStrike) : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
    try
    {
#ifdef HANDLE
        _phSuperFundPayoff = NULL;
#endif
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        QuantLib::Real _secondStrike = (QuantLib::Real)ValueHelper::Convert (secondStrike); //d
        _ppSuperFundPayoff = new boost::shared_ptr<QuantLib::SuperFundPayoff> (new QuantLib::SuperFundPayoff ( _strike,  _secondStrike ));
        SetStrikedTypePayoff (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (boost::shared_ptr<QuantLib::SuperFundPayoff>& childNative, Object^ owner) : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
#ifdef HANDLE
	_phSuperFundPayoff = NULL;
#endif
	_ppSuperFundPayoff = &childNative;
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
}
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (QuantLib::SuperFundPayoff& childNative, Object^ owner) : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
#ifdef HANDLE
	_phSuperFundPayoff = NULL;
#endif
	_ppSuperFundPayoff = new boost::shared_ptr<QuantLib::SuperFundPayoff> (&childNative);
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
    _SuperFundPayoffOwner = owner;
    _StrikedTypePayoffOwner = owner;
}

Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (CSuperFundPayoff^ copy) : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
#ifdef HANDLE
	_phSuperFundPayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSuperFundPayoff = new boost::shared_ptr<QuantLib::SuperFundPayoff> (copy->GetShared());
        _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
    }
}
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (PLATFORM::Type^ t) : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
#ifdef HANDLE
	_phSuperFundPayoff = NULL;
#endif
	if (!t->IsSubclassOf(CSuperFundPayoff::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (QuantLib::Handle<QuantLib::SuperFundPayoff>& childNative, Object^ owner)  : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
	_phSuperFundPayoff = &childNative;
	_ppSuperFundPayoff = &static_cast<boost::shared_ptr<QuantLib::SuperFundPayoff>>(childNative.currentLink());
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
    _SuperFundPayoffOwner = owner;
}
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (QuantLib::Handle<QuantLib::SuperFundPayoff> childNative)  : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
	_phSuperFundPayoff = &childNative;
	_ppSuperFundPayoff = &static_cast<boost::shared_ptr<QuantLib::SuperFundPayoff>>(childNative.currentLink());
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CSuperFundPayoff::CSuperFundPayoff (QuantLib::SuperFundPayoff childNative)  : CStrikedTypePayoff(CSuperFundPayoff::typeid)
{
#ifdef HANDLE
	_phSuperFundPayoff = NULL;
#endif
	_ppSuperFundPayoff = new boost::shared_ptr<QuantLib::SuperFundPayoff> (new QuantLib::SuperFundPayoff (childNative));
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppSuperFundPayoff));
}
#endif

Cephei::QL::Instruments::CSuperFundPayoff::~CSuperFundPayoff ()
{
    if (_ppSuperFundPayoff != NULL)
    {
	    delete _ppSuperFundPayoff;
        _ppSuperFundPayoff = NULL;
    }
}
Cephei::QL::Instruments::CSuperFundPayoff::!CSuperFundPayoff ()
{
    if (_ppSuperFundPayoff != NULL)
    {
	    delete _ppSuperFundPayoff;
    }
}
QuantLib::SuperFundPayoff& Cephei::QL::Instruments::CSuperFundPayoff::GetReference ()
{
    if (_ppSuperFundPayoff == NULL) throw REFNEW NativeNullException ();
	return **_ppSuperFundPayoff;
}
boost::shared_ptr<QuantLib::SuperFundPayoff>& Cephei::QL::Instruments::CSuperFundPayoff::GetShared ()
{
    if (_ppSuperFundPayoff == NULL) throw REFNEW NativeNullException ();
	return *_ppSuperFundPayoff;
}
QuantLib::SuperFundPayoff* Cephei::QL::Instruments::CSuperFundPayoff::GetPointer ()
{
    if (_ppSuperFundPayoff == NULL) throw REFNEW NativeNullException ();
	return &**_ppSuperFundPayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SuperFundPayoff>& Cephei::QL::Instruments::CSuperFundPayoff::GetHandle ()
{
	if (_phSuperFundPayoff == NULL)
	{
		_phSuperFundPayoff = new Handle<QuantLib::SuperFundPayoff> (*_ppSuperFundPayoff);
	}
	return *_phSuperFundPayoff;
}
#endif
bool Cephei::QL::Instruments::CSuperFundPayoff::HasNative () 
{
	return (_ppSuperFundPayoff != NULL);
}

String^ Cephei::QL::Instruments::CSuperFundPayoff::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppSuperFundPayoff)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CSuperFundPayoff::SecondStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSuperFundPayoff)->secondStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ISuperFundPayoff^ Cephei::QL::Instruments::CSuperFundPayoff_Factory::Create (Double strike, Double secondStrike)
{
    return REFNEW CSuperFundPayoff ( strike,  secondStrike);
}
